package com.zhhs.project.act.appactivity.controller;



import com.zhhs.framework.web.domain.AjaxResult;
import com.zhhs.project.act.activity.domian.ActActivityShareVo;
import com.zhhs.project.act.activity.service.IActActivityService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/wecom/mini-app")
public class AppActivityShareController {

    @Autowired
    private WxMpService wxMpService;
    @Autowired
    private IActActivityService actActivityService;

    @PostMapping("/getJssdkConfig")
    @ResponseBody
    public AjaxResult getJsonApi(String url, String activityId) {
        log.info("url:{}",url);
        log.info("activityId:{}",activityId);
        try {

            WxJsapiSignature jsapiSignature = wxMpService.createJsapiSignature(url);
            log.info("jsapiSignature:{}",jsapiSignature);
            Map<String,Object> param = new HashMap();
            param.put("js", jsapiSignature);

            //获取后台参数
            ActActivityShareVo actActivity = actActivityService.getJssdkConfig(Long.valueOf(activityId));
            String shareTitle =actActivity.getShareTitle();
            String shareDesc = actActivity.getShareDesc();
            String shareImgUrl = actActivity.getShareImgUrl();
            param.put("shareTitle", shareTitle);
            param.put("shareDesc", shareDesc);
            param.put("shareImgUrl", shareImgUrl);
            param.put("shareLink", url);
            log.info("4444");
            return AjaxResult.success(param);
        } catch (WxErrorException e) {
            log.info("WxErrorException:{}",e.getLocalizedMessage());
            return AjaxResult.error(e.getLocalizedMessage());
        }
    }
}
